package com.zzx.threekingdom.view;

import com.zzx.threekingdom.card.Card;
import com.zzx.threekingdom.card.EquipmentCard;
import com.zzx.threekingdom.card.PatternCard;
import com.zzx.threekingdom.card.EquipmentCard.EquipmentCardType;
import com.zzx.threekingdom.card.Pattern.Points;
import com.zzx.threekingdom.card.Pattern;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View.MeasureSpec;
import android.widget.LinearLayout;
import android.widget.TextView;


public class PlayerEquipmentView extends LinearLayout 
{
    public interface EquipmentReplace
    {
        public void onReplace(Card oldCard);
    }
    
    public TextView weaponView;
    public TextView armorView;
    public TextView plusHorseView;
    public TextView minusHorseView;
    
    public EquipmentCard weaponCard = null;
    public EquipmentCard armorCard = null;
    public EquipmentCard plusHorseCard = null;
    public EquipmentCard minusHorseCard = null;
    
    private EquipmentReplace listener = null;
    
    public PlayerEquipmentView(Context context)
    {
        this(context, null);
    }
    public PlayerEquipmentView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        setOrientation(VERTICAL);
        addView(weaponView = new TextView(context), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        addView(armorView = new TextView(context), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        addView(plusHorseView = new TextView(context), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        addView(minusHorseView = new TextView(context), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        clearView();

        String s = (new EquipmentCard(EquipmentCardType.weapon, Pattern.Color.spade, Points.ace, "青龙偃月刀")).getPatternText();
        TextView view = new TextView(context);
        view.setText(s);
        final int freeSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        view.measure(freeSpec, freeSpec);
        setMinimumWidth(view.getMeasuredWidth());
    }
    
    public void addEquipment(EquipmentCard card)
    {
        switch(card.getType())
        {
            case weapon: addWeapon(card); break;
            case armor: addArmor(card); break;
            case addhorse: addPlusHorse(card); break;
            case reducehorse: addMinusHorse(card); break;
            default: break;
        }
    }
    public void clearView()
    {
        listener = null;
        weaponView.setBackgroundColor(Color.DKGRAY);
        armorView.setBackgroundColor(Color.DKGRAY);
        plusHorseView.setBackgroundColor(Color.DKGRAY);
        minusHorseView.setBackgroundColor(Color.DKGRAY);
        weaponView.setText("");
        armorView.setText("");
        plusHorseView.setText("");
        minusHorseView.setText("");
        weaponCard = null;
        armorCard = null;
        plusHorseCard = null;
        minusHorseCard = null;
    }
    
    public void setEquipText(TextView tv, EquipmentCard card)
    {
        if(card!=null)
        {
            tv.setTextColor(card.isRed()==true?Color.RED:Color.BLACK);
            tv.setText(card.getPatternText());
        }
        else
            tv.setText("");
    }
    
    public void setReplaceListener(EquipmentReplace er) { listener = er; }
    public void notifyReplaceListener(EquipmentCard card)
    {
        if(listener!=null)
            listener.onReplace(card);
    }
    
    public void addWeapon(EquipmentCard card)
    {
        if(weaponCard!=null)
            notifyReplaceListener(weaponCard);
        setEquipText(weaponView, card);
        weaponCard = card;
    }
    public void addArmor(EquipmentCard card)
    {
        if(armorCard!=null)
            notifyReplaceListener(armorCard);
        setEquipText(armorView, card);
        armorCard = card;
    }
    public void addPlusHorse(EquipmentCard card)
    {
        if(plusHorseCard!=null)
            notifyReplaceListener(plusHorseCard);
        setEquipText(plusHorseView, card);
        plusHorseCard = card;
    }
    public void addMinusHorse(EquipmentCard card)
    {
        if(minusHorseCard!=null)
            notifyReplaceListener(minusHorseCard);
        setEquipText(minusHorseView, card);
        minusHorseCard = card;
    }
    
    public EquipmentCard getWeapon() { return weaponCard; }
    public EquipmentCard getArmor() { return armorCard; }
    public EquipmentCard getPlusHorse() { return plusHorseCard; }
    public EquipmentCard getMinusHorse() { return minusHorseCard; }
}
